package com.example.final_project.Usr.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.Comm.entry.CommContent;
import com.example.final_project.R;
import com.example.final_project.Usr.adapter.WorkAdapter;

import java.util.List;

public class WorkActivity extends AppCompatActivity {

    private Context context;
    private String name;
    private RecyclerView recyclerView;
    private ImageView back;
    public static final int COMM = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work2);

        findById();
        SharedPreferences sp = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        name = sp.getString("name","");
        getData();

        //返回
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(WorkActivity.this,AuthorActivity.class);
                startActivity(intent);
            }
        });

    }

    Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case COMM:
                    List<CommContent> commContents = (List<CommContent>) msg.obj;
                    WorkAdapter workAdapter = new WorkAdapter(commContents,context);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
                    recyclerView.setAdapter(workAdapter);
                    recyclerView.setLayoutManager(linearLayoutManager);
                    break;
            }

        }
    };

    private void getData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/commcontent")
                .bind(context)
                .addUrlPara("name",name)
                .setOnResList(CommContent.class,comm ->{
                    Message message = Message.obtain();
                    message.what = COMM;
                    message.obj = comm;
                    handler.sendMessage(message);
                })
                .get();
    }


    private void findById(){
        context = this.getBaseContext();
        recyclerView = this.findViewById(R.id.user_author_work);
        back = this.findViewById(R.id.user_author_work_back);
    }

}